package com.controller;



import com.entity.StoreHouse;
import com.entity.base.Result;
import com.service.StoreHouseService;
import com.service.oss.OSSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/admin")
@Slf4j
@Api(tags = "管理员相关接口", description = "提供相关的 Rest API")
public class AdminController {

    @Autowired
    StoreHouseService storeHouseService;
    @Autowired
    OSSClient ossClient;

    @ApiOperation("创建资源库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "team", value = "班级实体", required = true, dataType = "user", paramType = "body")
    })
    @GetMapping("/createStoreHouse")//创建公共资源库
    public Result createStoreHouse(String id){
        try {
            StoreHouse storeHouse = new StoreHouse();
            storeHouse.setId(Long.valueOf(id));
            storeHouse.setStatus(1);
            storeHouse.setRoleId(2);
            ossClient.mkDir("user_space/"+storeHouse.getId().toString()+"/");
            if(storeHouseService.save(storeHouse)) return Result.success(storeHouse);
            return Result.error("创建失败");
        }catch (Exception e){
            log.error(e.toString());
            return Result.error(e.toString());
        }
    }
    @ApiOperation("删除资源库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeHouse", value = "资源库", required = true, dataType = "user", paramType = "body")
    })
    @PostMapping("/deleteStoreHouse")
    public Result deleteStoreHouse(@RequestBody StoreHouse storeHouse){
        String path = System.getProperty("user.dir")+storeHouse.getPath();
            storeHouseService.removeById(storeHouse.getId());
            return Result.success();

    }

}
